import { useWebMediaStore } from './media';
import { useWebSiteStore } from './site'
import { defineStore, acceptHMRUpdate } from 'pinia'
import { MEDIAS } from '@/const/web/'
import { saveImgToServer, getMediaDataToServer } from '@/service/web/user'
declare interface IWebUpload {
  fileUploadData: IFileUploadData
}

class FileUploadData implements IFileUploadData {
  site_id: string = ''
  type: number = 0
  AWSAccessKeyId: string = "SINA000000ROQDH4Z6NW"
  key: string = "upload\/biz\/1\/${filename}"
  acl: string = "public-read"
  success_action_status: string = "201"
  'Content-Type': string = "application\/octet-stream"
  'Content-Disposition': string = "attachment; filename=${filename}"
  uid: string = ""
  Policy: string = ""
  Signature: string = "E81D+t4N6G+j+Ud9zE+Q8Z8pqYE="
}

export const useWebUploadStore = defineStore('webUpload', {
  state: (): IWebUpload => {
    return {
      fileUploadData: new FileUploadData()
    }
  },
  getters: {
      
  },
  actions: {
    setFileUploadData(data: FileUploadData) {
      data.key = data.key.split("$")[0]
      const useWebSite = useWebSiteStore()
      this.fileUploadData = _.merge({ site_id: useWebSite.id, type: MEDIAS.image.type }, data)
    },
    saveImgUpload(params: IFileUploadItem) {
      const type = params.type
      saveImgToServer(params, () => {
        const params_ = { page: 1, pageSize: 20, type: type }
        getMediaDataToServer(params_, (data: any) => {
          const data_ = {}
          data_[params_.type] = data
          data_[params_.type].isResetList = true
          const useWebMedia = useWebMediaStore()
          useWebMedia.setMediaData(data_)
        })
      })
    },
  },
})

if (import.meta.hot) {
  import.meta.hot!.accept(acceptHMRUpdate(useWebUploadStore, import.meta.hot))
}